package com.eastfair.venuebooking.controller;

import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.common.api.CollectRecordFeign;
import com.eastfair.common.api.TransactionFeign;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.pay.dto.*;
import com.eastfair.pay.enums.*;
import com.eastfair.pay.vo.CollectPageDetailVO;
import com.eastfair.pay.vo.CollectRecordVO;
import com.eastfair.venuebooking.entity.ContractManage;
import com.eastfair.venuebooking.service.ContractManageService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * 认款记录
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/collectRecord")
@Api(value = "CollectRecord", tags = "认款记录")
@PreAuth(replace = "pay:collectRecord:")
public class CollectRecordController {

    @Resource
    private CollectRecordFeign collectRecordFeign;
    @Resource
    private TransactionFeign transactionFeign;
    @Resource
    private ContractManageService contractManageService;
    /**
     * 认领
     * @return
     */
    @SysLog("认款")
    @PostMapping("/collect")
    public R collect(@RequestBody CollectDTO collectDTO){

        R<?> result = collectRecordFeign.collect(collectDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }

        ContractManage contractManage = contractManageService.getById(collectDTO.getBusinessId());
        TransactionDTO transactionDTO = new TransactionDTO();
        transactionDTO.setExhibitionId(contractManage.getExhibitionId());
        transactionDTO.setExhibitionName(contractManage.getExhibitionName());
        transactionDTO.setOrderAmount(contractManage.getContractAmount());
        transactionDTO.setOrderNum(contractManage.getContractNumber());
//        transactionDTO.setOrderStatus();
        transactionDTO.setOrderTime(contractManage.getCreateTime());
        transactionDTO.setOrderType(InvoiceApplyBusinessTypeEnum.H.getCode());
//        transactionDTO.setPayee();
//        transactionDTO.setPayeeId();
        transactionDTO.setPayer(contractManage.getCutomerName());
        transactionDTO.setPaymentChannel(PayChannelEnum.CORPORATE_TRANSGER.getCode());
//        transactionDTO.setServiceProviderId();
//        transactionDTO.setServiceProviderName();
//        transactionDTO.setServiceProviderType();
        transactionDTO.setPayType(PayTypeEnum.TRANSFER_ACCOUNT.getCode());
//        transactionDTO.setSiteName();
        transactionDTO.setTransactionState(TransactionStatusEnum.PAYED.getCode());
        transactionDTO.setTransactionType(TranstionTypeEnum.PAYED.getCode());
        transactionDTO.setTranstionAmount(collectDTO.getCollectAmount());
        transactionDTO.setTranstionTime(LocalDateTime.now());
        R<?> transactionResult = transactionFeign.save(transactionDTO);
        if (!transactionResult.getIsSuccess()) {
            throw BizException.wrap(transactionResult.getCode(), transactionResult.getMsg());
        }
        return R.success();
    }

    /**
     * 取消认领
     * @return
     */
    @SysLog("取消认款")
    @PostMapping("/uncollect")
    public R uncollect(@RequestBody UnCollectDTO unCollectDTO){

        R<?> result = collectRecordFeign.uncollect(unCollectDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }

//        R<CollectRecordVO> res = collectRecordFeign.getCollectRecordById(unCollectDTO.getCollectRecordId());
//
//        if (!res.getIsSuccess()){
//            throw BizException.wrap(res.getCode(), res.getMsg());
//        }
//        CollectRecordVO data = res.getData();
//        ContractManage contractManage = contractManageService.getById(unCollectDTO.getBusinessId());
//        TransactionDTO transactionDTO = new TransactionDTO();
//        transactionDTO.setExhibitionId(contractManage.getExhibitionId());
//        transactionDTO.setExhibitionName(contractManage.getExhibitionName());
//        transactionDTO.setOrderAmount(contractManage.getContractAmount());
//        transactionDTO.setOrderNum(contractManage.getContractNumber());
////        transactionDTO.setOrderStatus();
//        transactionDTO.setOrderTime(contractManage.getCreateTime());
//        transactionDTO.setOrderType(InvoiceApplyBusinessTypeEnum.H.getCode());
////        transactionDTO.setPayee();
////        transactionDTO.setPayeeId();
//        transactionDTO.setPayer(contractManage.getCutomerName());
//        transactionDTO.setPaymentChannel(PayChannelEnum.CORPORATE_TRANSGER.getCode());
////        transactionDTO.setServiceProviderId();
////        transactionDTO.setServiceProviderName();
////        transactionDTO.setServiceProviderType();
//        transactionDTO.setPayType(PayTypeEnum.TRANSFER_ACCOUNT.getCode());
////        transactionDTO.setSiteName();
//        transactionDTO.setTransactionState(TransactionStatusEnum.HAS_REFUND.getCode());
//        transactionDTO.setTransactionType(TranstionTypeEnum.REFUND.getCode());
//        transactionDTO.setTranstionAmount(data.getCollectAmount());
//        transactionDTO.setTranstionTime(LocalDateTime.now());
//        R<?> transactionResult = transactionFeign.save(transactionDTO);
//        if (!transactionResult.getIsSuccess()) {
//            throw BizException.wrap(transactionResult.getCode(), transactionResult.getMsg());
//        }
        return R.success();
    }

    /**
     * 获取付款计划认领记录
     * @param paymentPlanId
     * @return
     */
    @RequestMapping(value="/queryCollectByPlanId/{paymentPlanId}",method=RequestMethod.GET)
    public R<List<CollectRecordVO>> queryCollectByPlanId(@PathVariable("paymentPlanId") Long paymentPlanId){

        R<List<CollectRecordVO>> result = collectRecordFeign.queryCollectByPlanId(paymentPlanId);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }

        return result;
    }

    /**
     * 获取付款记录认领记录
     * @param recordId
     * @return
     */
    @RequestMapping(value="/queryCollectByRecordId/{recordId}",method=RequestMethod.GET)
    public R<List<CollectRecordVO>> queryCollectByRecordId(@PathVariable("recordId") Long recordId){

        R<List<CollectRecordVO>> result = collectRecordFeign.queryCollectByRecordId(recordId);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }

        return result;
    }


    /**
     * 通过合同获取认领页面详情
     * @param businessId
     * @return
     */
    @RequestMapping(value="/queryCollectPageDetail/{businessId}",method=RequestMethod.GET)
    public R<CollectPageDetailVO> getCollectPageDetail(@PathVariable("businessId") Long businessId){

        R<CollectPageDetailVO> result = collectRecordFeign.getCollectPageDetail(businessId);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }

        return result;
    }

}
